package team.bluepen.order.util;

import java.util.Random;

/**
 * 字符串实用类
 * @author Kai
 */
public final class StringUtil {

    private StringUtil() { }

    /**
     * 字符串判断是否为空（避免空指针）
     * @param s 需要判断的字符串
     * @return 为空则为 {@code true}
     */
    public static boolean isEmpty(String s) {
        return s == null || s.isEmpty();
    }

    /**
     * 数字转换为定长字符串，不足位补零
     * @param number 数字
     * @param length 长度
     * @return 转换后字符串
     */
    public static String convert(long number, int length) {
        if (length <= 0) {
            return "";
        }
        StringBuilder res = new StringBuilder(String.valueOf(number));
        if (res.length() == length) {
            return res.toString();
        }
        if (res.length() > length) {
            res = new StringBuilder(res.substring(res.length() - length, res.length()));
        } else {
            while (res.length() < length) {
                res.insert(0, "0");
            }
        }
        return res.toString();
    }

    public static String getRandomString(int length){
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder stringBuffer = new StringBuilder();
        for(int i = 0; i < length; i++){
            int number = random.nextInt(62);
            stringBuffer.append(str.charAt(number));
        }
        return stringBuffer.toString();
    }
}
